package com.example.demo.controller;

import com.example.demo.VO.ResultVO;
import com.example.demo.pojo.MessageEntity;
import com.example.demo.pojo.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.util.locale.provider.DateFormatSymbolsProviderImpl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Resource
    UserService service;

@PostMapping(value = "/Register")
    public ResultVO getRegister(@RequestParam(value = "userPassword",required=true,defaultValue="") String account,
                                @RequestParam(value = "userName",required=true,defaultValue="") String userName,
                                @RequestParam(value = "userType",required=true,defaultValue="") String userType
                              )
    {

        UserEntity user = new UserEntity();
        user.setUserPassword(account);
        user.setUserName(userName);
        user.setUserType(userType);
        service.register(user);
        Map json = new HashMap();
        ResultVO resultVO = new ResultVO();
         json.put("user",user);

        resultVO.setData(json);
          return resultVO;



    }


    @PostMapping(value = "/sendMessage")
    public ResultVO SendMessage(@RequestParam(value = "sendId",required=true,defaultValue="") int account,
                                @RequestParam(value = "sendName",required=true,defaultValue="") String userName,
                                @RequestParam(value = "sendTime",required=true,defaultValue="") String sendTime,
                                @RequestParam(value = "sendNote",required=true,defaultValue="") String sendNote
    )
    {

        MessageEntity message =new MessageEntity();
        message.setSendId(account);
        message.setSendName( userName);
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(day));
        message.setSendTime(df.format(day));
        message.setSendNote(sendNote);
        service.insertMessage(message );
        Map json = new HashMap();
        ResultVO resultVO = new ResultVO();
        json.put("message",message);

        resultVO.setData(json);
        return resultVO;



    }

    @PostMapping(value = "/check")
    public  ResultVO  check(
                                @RequestParam(value = "userName",required=true,defaultValue="") String userName,
                                @RequestParam(value = "password",required=true,defaultValue="") String password
    )
    {

        Map json = new HashMap();
        ResultVO resultVO = new ResultVO();

        json.put("check", service.check(userName,password));
        resultVO.setData(json);
        return resultVO ;



    }











}
